//
//  PickerView.swift
//  zob
//
//  Created by A-Jay on 1/4/2017.
//  Copyright © 2017年 A-Jay. All rights reserved.
//

import UIKit

class PickerView: UIView, UIPickerViewDelegate,UIPickerViewDataSource{
    
    //block
    var pickerBlock:((Int, String) -> ())?
    
    let pickerHeight : CGFloat = 216.0;
    let buttonViewHeight : CGFloat = 40.0;
    let buttonWidth : CGFloat = 80.0;
    
    var dataSource : Array<String> = []
    
    ///-----------------------------------------------------------
    /// MARK: - Getters And Setters ( getter 和 setter  )
    ///-----------------------------------------------------------
    
    private lazy var bgView:UIView = {
        let view = UIView()
        self.addSubview(view)
        return view
    }()
    
    private lazy var buttonBGView:UIView  = {
        let view = UIView()
        view.layer.borderColor = UIColor.hexStringToColor(hexString: "#919191").cgColor;
        view.layer.borderWidth = 0.5;
        self.bgView.addSubview(view)
        return view
    }()
    
    private lazy var canCelButton:UIButton = {
        let button = UIButton.creatTitleButton("取消", "#e3483d", 14)
        button.addTarget(self, action: #selector(clickCancelButton(sender:)), for: .touchUpInside)
        self.buttonBGView.addSubview(button)
        return button;
    }()
    
    private lazy var doneButton:UIButton = {
        let button = UIButton.creatTitleButton("完成", "#e3483d", 14)
        button.addTarget(self, action: #selector(clickDoneButton(sender:)), for: .touchUpInside)
        self.buttonBGView.addSubview(button)
        return button;
    }()
    
    public lazy var pickerView:UIPickerView = {
        // 创建UIPickerView控件
        let picker = UIPickerView()
        self.addSubview(picker)
        
        // 设置代理和数据源
        picker.delegate = self;
        picker.dataSource = self
        
        // 设置选择框的默认值
//        picker.selectRow(1, inComponent: 0, animated: true)
//        picker.selectRow(2, inComponent: 1, animated: true)
//        picker.selectRow(3, inComponent: 2, animated: true)
        
        return picker
    }()
    
    ///-----------------------------------------------------------
    /// MARK: - Life Cycle ( 生命周期方法 )
    ///-----------------------------------------------------------
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame:CGRect){
        super.init(frame: frame);
        
        self.bgView.frame = CGRect(x: 0, y: self.bounds.size.height - pickerHeight - buttonViewHeight, width: UIScreen.main.bounds.size.width, height: pickerHeight + buttonViewHeight)
        self.buttonBGView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: buttonViewHeight)
        self.canCelButton.frame = CGRect(x: 0, y: 0, width: buttonWidth, height: buttonViewHeight)
        self.doneButton.frame = CGRect(x: UIScreen.main.bounds.size.width - buttonWidth, y: 0, width: buttonWidth, height: buttonViewHeight)
        self.pickerView.frame = CGRect(x: 0, y: buttonViewHeight, width: UIScreen.main.bounds.size.width, height: pickerHeight)
        
        
    }
    
    ///-----------------------------------------------------------
    /// MARK: - Private Methods ( 私有方法 )
    ///-----------------------------------------------------------
    
    
    
    ///-----------------------------------------------------------
    /// MARK: - Public Methods ( 公有方法 ))
    ///-----------------------------------------------------------
    
    
    ///-----------------------------------------------------------
    /// MARK: - Event Responses ( 事件响应 )
    ///-----------------------------------------------------------
    
    /**
     获取选择框被选中的索引的方法
     */
    
    func clickCancelButton(sender: UIButton) {
        pickerBlock!(-1,"")
    }
    
    func clickDoneButton(sender: UIButton) {
        pickerBlock!(pickerView.selectedRow(inComponent: 0),dataSource[pickerView.selectedRow(inComponent: 0)])
//        print("被选中的索引为：\(pickerView.selectedRow(inComponent: 0))、\(pickerView.selectedRow(inComponent: 1))、\(pickerView.selectedRow(inComponent: 2))")
    }
    
    ///-----------------------------------------------------------
    /// MARK: - UIPickerViewDataSource
    ///-----------------------------------------------------------
    
    /**
     设置选择框的列数
     */
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    /**
     设置选择框的行数
     */
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return dataSource.count
    }
    
    ///-----------------------------------------------------------
    /// MARK: - UIPickerViewDelegate
    ///-----------------------------------------------------------
    
    /**
     设置选择框各选项的内容
     */
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        
        
        return dataSource[row]
    }
    
    ///-----------------------------------------------------------
    /// MARK: - Notification Responses ( 通知响应 )
    ///-----------------------------------------------------------
    
    
    ///-----------------------------------------------------------
    /// MARK: - Network Data ( 网络请求处理 )
    ///-----------------------------------------------------------
    
}
